<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring Boot 数据源集成深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .serif-font {
            font-family: 'Noto Serif SC', serif;
        }
        .gradient-bg {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            overflow-x: auto;
        }
        .code-block pre {
            padding: 1.5rem;
            margin: 0;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 0.9rem;
            line-height: 1.6;
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
        }
        .section-divider {
            position: relative;
            text-align: center;
            margin: 4rem 0;
        }
        .section-divider::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 0;
            right: 0;
            height: 1px;
            background: linear-gradient(to right, transparent, #e5e7eb, transparent);
        }
        .section-divider span {
            background: white;
            padding: 0 2rem;
            position: relative;
            color: #9ca3af;
        }
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        .animate-fadeInUp {
            animation: fadeInUp 0.8s ease-out;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="gradient-bg text-white py-20 px-6">
        <div class="max-w-6xl mx-auto animate-fadeInUp">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 serif-font">
                Spring Boot 数据源集成
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">
                深入解析自动配置机制与最佳实践
            </p>
            <div class="flex items-center space-x-6 text-lg">
                <span class="flex items-center">
                    <i class="fas fa-database mr-2"></i>
                    数据源配置
                </span>
                <span class="flex items-center">
                    <i class="fas fa-cogs mr-2"></i>
                    自动化集成
                </span>
                <span class="flex items-center">
                    <i class="fas fa-rocket mr-2"></i>
                    高性能连接池
                </span>
            </div>
        </div>
    </section>

    <!-- Introduction -->
    <section class="max-w-4xl mx-auto px-6 py-16">
        <div class="bg-white rounded-2xl shadow-xl p-8 md:p-12 card-hover">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap serif-font">S</span>pring Boot 是基于 Spring 框架构建的快速开发平台，它提供了一种自动化配置的方式来简化各种常见的开发任务，其中包括数据源的配置和集成。在 Spring Boot 中，数据源的集成不仅仅依赖于 Spring Data，还通过其强大的自动配置机制帮助开发者轻松地集成和使用不同类型的数据库。Spring Boot 默认支持多种数据源的配置，例如 HikariCP、Tomcat JDBC 和 DBCP2。
            </p>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 pb-20">
        
        <!-- Section 1: 自动配置实现 -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="bg-gradient-to-r from-purple-600 to-indigo-600 text-white rounded-full w-12 h-12 flex items-center justify-center mr-4">
                    <span class="font-bold text-xl">1</span>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">Spring Boot 数据源自动配置的实现</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-lg p-8">
                <p class="text-gray-700 mb-6 leading-relaxed">
                    在 Spring Boot 中，数据源的集成是通过 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">DataSourceAutoConfiguration</code> 类实现的，该类位于 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">org.springframework.boot.autoconfigure.jdbc</code> 包中。它的主要作用是自动配置数据源。Spring Boot 的自动配置是基于 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">@EnableAutoConfiguration</code> 注解和 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">@Configuration</code> 注解来实现的。
                </p>
                
                <h3 class="text-xl font-semibold mb-4 text-gray-800">
                    <i class="fas fa-code mr-2 text-purple-600"></i>自动配置流程
                </h3>
                
                <div class="code-block mb-6">
                    <pre>@Configuration
@ConditionalOnClass(DataSource.class)
@AutoConfigureBefore(DataSourceTransactionManagerAutoConfiguration.class)
public class DataSourceAutoConfiguration {
    
    @Bean
    @Primary
    @ConditionalOnMissingBean(DataSource.class)
    public DataSource dataSource(DataSourceProperties properties) {
        HikariDataSource dataSource = new HikariDataSource();
        properties.configure(dataSource);
        return dataSource;
    }
}</pre>
                </div>
                
                <div class="bg-blue-50 border-l-4 border-blue-500 p-6 rounded-r-lg">
                    <h4 class="font-semibold text-blue-900 mb-3">
                        <i class="fas fa-info-circle mr-2"></i>关键注解说明
                    </h4>
                    <ul class="space-y-2 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-blue-500 mr-2 mt-1"></i>
                            <span><code class="bg-blue-100 text-blue-700 px-2 py-1 rounded text-sm">@ConditionalOnClass(DataSource.class)</code> 确保只有在类路径中存在数据库连接的相关类时才会启用数据源自动配置。</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-blue-500 mr-2 mt-1"></i>
                            <span><code class="bg-blue-100 text-blue-700 px-2 py-1 rounded text-sm">@AutoConfigureBefore</code> 确保数据源在事务管理器配置之前进行配置。</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-blue-500 mr-2 mt-1"></i>
                            <span><code class="bg-blue-100 text-blue-700 px-2 py-1 rounded text-sm">@Bean</code> 和 <code class="bg-blue-100 text-blue-700 px-2 py-1 rounded text-sm">@Primary</code> 注解表示默认配置的数据源会被应用到 Spring 容器中。</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- Architecture Diagram -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-lg p-8">
                <h3 class="text-2xl font-bold mb-6 text-center text-gray-800">数据源配置架构图</h3>
                <div class="mermaid">
                    graph TB
                        A[Spring Boot 应用启动] --> B[自动配置扫描]
                        B --> C{检查 DataSource.class}
                        C -->|存在| D[DataSourceAutoConfiguration]
                        C -->|不存在| E[跳过数据源配置]
                        D --> F[读取 DataSourceProperties]
                        F --> G[创建 HikariDataSource]
                        G --> H[注册到 Spring 容器]
                        H --> I[配置事务管理器]
                        I --> J[应用就绪]
                        
                        style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style J fill:#48bb78,stroke:#fff,stroke-width:2px,color:#fff
                        style D fill:#ed8936,stroke:#fff,stroke-width:2px,color:#fff
                </div>
            </div>
        </section>

        <!-- Section 2: 数据源初始化与配置 -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="bg-gradient-to-r from-purple-600 to-indigo-600 text-white rounded-full w-12 h-12 flex items-center justify-center mr-4">
                    <span class="font-bold text-xl">2</span>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">数据源的初始化与配置</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <h3 class="text-xl font-semibold mb-4 text-gray-800">
                        <i class="fas fa-cog mr-2 text-purple-600"></i>DataSourceProperties 配置类
                    </h3>
                    <div class="code-block">
                        <pre>@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceProperties {
    private String url;
    private String username;
    private String password;
    private String driverClassName;
    private String hikari;

    public void configure(HikariDataSource dataSource) {
        dataSource.setJdbcUrl(this.url);
        dataSource.setUsername(this.username);
        dataSource.setPassword(this.password);
        dataSource.setDriverClassName(this.driverClassName);
    }
}</pre>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <h3 class="text-xl font-semibold mb-4 text-gray-800">
                        <i class="fas fa-file-code mr-2 text-purple-600"></i>配置文件示例
                    </h3>
                    <div class="code-block">
                        <pre># 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.hikari.maximum-pool-size=10</pre>
                    </div>
                    <p class="mt-4 text-sm text-gray-600">
                        通过配置文件，<code class="bg-purple-100 text-purple-700 px-2 py-1 rounded text-xs">DataSourceProperties</code> 会自动注入数据库连接信息。
                    </p>
                </div>
            </div>
        </section>

        <!-- Section 3: HikariCP 配置 -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="bg-gradient-to-r from-purple-600 to-indigo-600 text-white rounded-full w-12 h-12 flex items-center justify-center mr-4">
                    <span class="font-bold text-xl">3</span>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">配置 HikariCP 作为数据源</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-lg p-8">
                <div class="mb-6">
                    <div class="flex items-center mb-4">
                        <i class="fas fa-bolt text-yellow-500 text-2xl mr-3"></i>
                        <p class="text-lg text-gray-700">
                            Spring Boot 默认使用 <span class="font-semibold text-purple-600">H